package com.sc.nft.admin.controller;

import cn.hutool.core.date.DateUtil;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.admin.request.StatusRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.entity.SysMember;
import com.sc.nft.entity.req.EcologyHatchExchangeConfRequest;
import com.sc.nft.entity.vo.AdminEcologyHatchExchangeConfVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.EcologyHatchExchangeConfService;
import com.sc.nft.service.EcologySmeltingService;
import com.sc.nft.service.UserEcologyHatchIncomeService;
import com.sc.nft.service.UserEcologyHatchUpperLimitService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-08-07
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/ecologyHatch/exchange")
@Api("兑换熔炼上限")
public class EcologyHatchExchangeConfController {


    private final EcologyHatchExchangeConfService ecologyHatchExchangeConfService;

    private final EcologySmeltingService ecologySmeltingService;

    private final UserEcologyHatchUpperLimitService userEcologyHatchUpperLimitService;
    private final UserEcologyHatchIncomeService userEcologyHatchIncomeService;
    private final Redisson redisson;



    @PostMapping(value = "/editConfigure", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增或者修改兑换熔炼上限配置", notes = "新增或者修改兑换熔炼上限配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editEcologyHatchExchangeConf(@Valid @RequestBody EcologyHatchExchangeConfRequest req) {
        ecologyHatchExchangeConfService.editEcologyHatchExchangeConf(req);
        return Result.success();
    }

    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "兑换熔炼上限配置详情", notes = "兑换熔炼上限配置详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminEcologyHatchExchangeConfVO> getAdminEcologyHatchExchangeConf(@Valid @RequestBody IdRequest req) {
        return Result.success(ecologyHatchExchangeConfService.findAdminEcologyHatchExchangeConfDetail(req.getId()));
    }



    @ApiOperation(value = "用户奖励数据转换至可消耗熔炼份数", notes = "用户奖励数据转换至可消耗熔炼份数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/remainingRewards", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result remainingRewardsCountToUsableCountScript(){

        SysMember member = SysMemberPool.getMember();
        if (!member.getId().equals(86L)) {
            throw new GlobalRunTimeException("无权限");
        }
        RLock lock = redisson.getLock("script" + member.getId());
        try {
            boolean res = lock.tryLock();
            if (res) {
                ecologySmeltingService.remainingRewardsCountToUsableCountScript();
            } else {
                return Result.fail("数据同步已经开始：同步人ID：" + member.getId());
            }
        } catch (Exception e) {
            throw new GlobalRunTimeException("数据处理异常" + e.getMessage());
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @ApiOperation(value = "结算周期奖励", notes = "结算周期奖励", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/settlePeriodicReward", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result settlePeriodicReward(){
        Date now = new Date();
        Date startTime = DateUtil.beginOfDay(now);
        Date endTime = DateUtil.endOfDay(now);
        userEcologyHatchIncomeService.settlePeriodicReward(startTime,endTime);
        return Result.success();
    }


}
